package stat

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"navi-mumbai/common/gensql"
)

var _ UserDepositDailyStatModel = (*customUserDepositDailyStatModel)(nil)

type (
	// UserDepositDailyStatModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserDepositDailyStatModel.
	UserDepositDailyStatModel interface {
		userDepositDailyStatModel
		SumAmount(ctx context.Context, payChannelId int64, start, end int64) (int64, error)
		SumSuccessUser(ctx context.Context, payChannelId int64, start, end int64) (int64, error)
		SumSuccessFlow(ctx context.Context, payChannelId int64, start, end int64) (int64, error)
	}

	customUserDepositDailyStatModel struct {
		*defaultUserDepositDailyStatModel
		genSql *gensql.GenSql
	}
)

// NewUserDepositDailyStatModel returns a model for the database table.
func NewUserDepositDailyStatModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) UserDepositDailyStatModel {
	return &customUserDepositDailyStatModel{
		defaultUserDepositDailyStatModel: newUserDepositDailyStatModel(conn, c, opts...),
		genSql:                           gensql.NewGenSql(),
	}
}

func (m *customUserDepositDailyStatModel) SumAmount(ctx context.Context, payChannelId int64, start, end int64) (int64, error) {
	q := fmt.Sprintf("select coalesce(sum(deposit_amount), 0) from %s ", m.table)
	m.genSql.InitSql(q)

	if start != 0 {
		m.genSql = m.genSql.Where("stat_day >= ?", start)
	}
	if end != 0 {
		m.genSql = m.genSql.Where("stat_day <= ?", end)
	}
	if payChannelId != 0 {
		m.genSql = m.genSql.Where("pay_channel_id = ?", payChannelId)
	}
	query, args, err := m.genSql.ToSql()
	if err != nil {
		return 0, err
	}
	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, args...)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}

func (m *customUserDepositDailyStatModel) SumSuccessUser(ctx context.Context, payChannelId int64, start, end int64) (int64, error) {
	q := fmt.Sprintf("select coalesce(sum(deposit_user_count), 0) from %s ", m.table)
	m.genSql.InitSql(q)

	if start != 0 {
		m.genSql = m.genSql.Where("stat_day >= ?", start)
	}
	if end != 0 {
		m.genSql = m.genSql.Where("stat_day <= ?", end)
	}
	if payChannelId != 0 {
		m.genSql = m.genSql.Where("pay_channel_id = ?", payChannelId)
	}
	query, args, err := m.genSql.ToSql()
	if err != nil {
		return 0, err
	}
	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, args...)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}

func (m *customUserDepositDailyStatModel) SumSuccessFlow(ctx context.Context, payChannelId int64, start, end int64) (int64, error) {
	q := fmt.Sprintf("select coalesce(sum(deposit_count), 0) from %s ", m.table)
	m.genSql.InitSql(q)
	if start != 0 {
		m.genSql = m.genSql.Where("stat_day >= ?", start)
	}
	if end != 0 {
		m.genSql = m.genSql.Where("stat_day <= ?", end)
	}
	if payChannelId != 0 {
		m.genSql = m.genSql.Where("pay_channel_id = ?", payChannelId)
	}
	query, args, err := m.genSql.ToSql()
	if err != nil {
		return 0, err
	}
	var resp int64
	err = m.QueryRowNoCacheCtx(ctx, &resp, query, args...)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}
